﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using ParaEditor.Common;
using ParaEditor.Domain.Entities;

namespace ParaEditor.DAO.ParaDAO
{
    public class Para4003DAO:BaseDAO
    {
        private static Para4003 p4003 = new Para4003();

        public Para4003DAO()
            : base(p4003)
        {
        }

        public ObservableCollection<Para4003> GetObjectByInfoVer(int infoVer)
        {
            ObservableCollection<Para4003> list = new ObservableCollection<Para4003>();
            IList iList = base.GetObjectByInfoVer(infoVer);
            foreach (Para4003 item in iList)
            {
                list.Add(item);
            }
            return list;
        }

        public ResultValue SaveObject(int areaNo, int val,Para4003 param)
        {
            ResultValue rValue = new ResultValue();
            if (base.IsExsit(param.para4003Pk.infoVer, param.para4003Pk.recordId))
            {
                //直接更新
                rValue = UpdateColumn(areaNo, val, param.para4003Pk.infoVer,param.para4003Pk.recordId);
            }
            else
            { 
                //先create
                rValue = base.CreateObject(param);
                //create成功
                if (rValue.Code == 0)
                {
                    rValue = UpdateColumn(areaNo, val, param.para4003Pk.infoVer, param.para4003Pk.recordId);
                }
                else
                {
                    rValue.Code = -1;
                    rValue.Msg = "插入一个4003对象失败!";
                }
            }
            return rValue;
        }

        private ResultValue UpdateColumn(int areaNo, int val, short infoVer,short recordid)
        {
            ResultValue rValue = new ResultValue();
            string columnName = "RATE_" + areaNo.ToString();
            try
            {
                base.UpdateColumn(columnName, val, infoVer, recordid);
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            return rValue;
        }
    }
}
